package com.mspbots.core;


import org.springframework.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * com.mspbots.web.RestMessageObject
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/8/20
 */
public class RestMessageObject extends HashMap<String, Object> {

    public RestMessageObject() {
        put("code", 200);
    }

    public static RestMessageObject error() {
        return error(HttpStatus.SERVICE_UNAVAILABLE, "Unknown exception, please contact administrator");
    }

    public static RestMessageObject error(String msg) {
        return error(HttpStatus.SERVICE_UNAVAILABLE, msg);
    }

    public static RestMessageObject error(HttpStatus code, String msg) {
        RestMessageObject r = new RestMessageObject();
        r.put("code", code.value());
        r.put("msg", msg);
        return r;
    }

    public static RestMessageObject ok(String msg) {
        RestMessageObject r = new RestMessageObject();
        r.put("msg", msg);
        return r;
    }

    public static RestMessageObject ok(Map<String, Object> map) {
        RestMessageObject r = new RestMessageObject();
        r.putAll(map);
        return r;
    }

    public static RestMessageObject ok() {
        return new RestMessageObject();
    }

    public static RestMessageObject apiError(String msg) {
        return error(HttpStatus.BAD_REQUEST, msg);
    }

    @Override
    public RestMessageObject put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
